package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.dto.GradeDto;
import com.wizz.trade.information.dto.page.GradePageDto;
import com.wizz.trade.information.entity.Grade;
import com.wizz.trade.information.service.GradeService;
import com.wizz.trade.information.service.VarietyService;
import com.wizz.trade.information.vo.GradeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资料模块/商品规格
 */
@RestController
@RequestMapping("/grade")
public class GradeController {

    @Resource
    private GradeService gradeService;

    @Resource
    private VarietyService varietyService;

    /**
     * 添加操作
     *
     * @param gradeDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('设置商品资料')")
    public ResponseResult addGrade(@RequestBody GradeDto gradeDto) {
        Grade grade = new Grade();
        BeanUtils.copyProperties(gradeDto, grade);
        gradeService.addGrade(grade);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 更新（编辑）操作
     *
     * @param gradeDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('设置商品资料')")
    public ResponseResult updateGrade(@RequestBody GradeDto gradeDto, @PathVariable("id") Integer id) {
        Grade grade = new Grade();
        BeanUtils.copyProperties(gradeDto, grade);
        grade.setId(id);
        gradeService.updateGrade(grade);
        return new ResponseResult(StatusCode.OK, "修改成功");
    }

    /**
     * 删除（撤销）操作
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('设置商品资料')")
    public ResponseResult deleteGrade(@PathVariable("id") Integer id) {
        gradeService.deleteGrade(id);
        return new ResponseResult(StatusCode.OK, "删除成功");
    }

    /**
     * 分页刷新、条件查询
     *
     * @param gradePageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('设置商品资料')")
    public ResponseResult<PageVo<GradeVo>> list(GradePageDto gradePageDto) {
        PageVo<Grade> rawPage = gradeService.getPage(gradePageDto);
        List<Grade> data = rawPage.getData();
        List<GradeVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            GradeVo gradeVo = new GradeVo();
            String varietyName = varietyService.getNameById(data.get(i).getVariety());
//            if (!StringUtils.hasLength(varietyName)) {
//                return new ResponseResult<>(StatusCode.ERROR, "请传递正确的品种ID");
//            }
            gradeVo.setVariety(varietyName);
            BeanUtils.copyProperties(data.get(i), gradeVo);
            dataVo.add(gradeVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 获取规格的下拉框选项
     * @param varietyId 品种id
     * @return 选项
     */
    @GetMapping("/options/{id}")
    public ResponseResult<List<Map<Integer, String>>> getOptions(@PathVariable("id") Integer varietyId) {
        List<Map<Integer, String>> options = gradeService.getOptions(varietyId);
        return new ResponseResult<>(StatusCode.OK, options);
    }
//
//    /**
//     * 根据品种查找该品种的素有
//     * @param varietyId
//     * @return
//     */
//    @GetMapping("/names/{id}")
//    public ResponseResult<List<Map<Integer, String>>> getNames(@PathVariable("id") Integer varietyId) {
//        List<Map<Integer, String>> names = gradeService.getNames(varietyId);
//        return new ResponseResult<>(StatusCode.OK, names);
//    }
//
//
//    @GetMapping("/list/by/variety/{id})")
//    public ResponseResult<List<GradeSimpleVo>> listByVariety(@PathVariable Integer id) {
//        QueryWrapper<Grade> gradeQueryWrapper = new QueryWrapper<>();
//        gradeQueryWrapper.eq("variety", id);
//        List<Grade> list = gradeService.list(gradeQueryWrapper);
//        ArrayList<GradeSimpleVo> resList = new ArrayList<>();
//        for (Grade grade : list) {
//            GradeSimpleVo gradeSimpleVo = new GradeSimpleVo();
//            BeanUtils.copyProperties(grade, gradeSimpleVo);
//            resList.add(gradeSimpleVo);
//        }
//        return new ResponseResult<>(StatusCode.OK, resList);
//
//    }
}
